package com.easipass.unifiedconfig.osgi;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import com.easipass.unifiedconfig.ConfigManager;
import com.easipass.unifiedconfig.DefaultConfigManager;
import com.easipass.unifiedconfig.core.ConfigDomain;
import com.easipass.unifiedconfig.unic.UnicConfigDomainFactory;

/**
 * 
 * @version $Id$
 * @author itea
 * @date 2010-2-25
 */
public class ConfigManagerActivator implements BundleActivator {

	final DefaultConfigManager cm = new DefaultConfigManager();
	
	ServiceRegistration serviceRegistration;
	
	public ConfigManagerActivator() {
		
	}
	
	public void start(BundleContext bundleContext) throws Exception {
		if(cm.getConfigDomain("unic") == null) {
			
			Map<String, String> option = new HashMap<String, String>();
			option.put("domain", "unic");
			
			String rPass = bundleContext.getProperty("com.easipass.unifiedconfig.unic.rootpassword");
			if(rPass == null) rPass = "root";
			option.put("unic.rootpassword", rPass);
			
			String pPath = bundleContext.getProperty("com.easipass.unifiedconfig.unic.persistence.path");
			if(pPath != null && pPath.trim().length() != 0)
				option.put("unic.persistence.path", pPath);
			
			ConfigDomain repo = new UnicConfigDomainFactory().getConfigDomain(option);
			cm.registerRepository( repo, repo.authenticate("root", "root"));
		}
		
		serviceRegistration = bundleContext.registerService(
				ConfigManager.class.getCanonicalName(), cm, new Properties());
	}

	public void stop(BundleContext bundleContext) throws Exception {
		// TODO Auto-generated method stub

	}

}
